package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.yipaiju.domain.DiscountHouse;
import com.ruoyi.yipaiju.domain.DiscountHouseSigningorder;
import com.ruoyi.yipaiju.domain.ForeclosedHouseSigningorder;
import com.ruoyi.yipaiju.domain.HouseUploadingOrder;
import com.ruoyi.yipaiju.service.IDiscountHouseService;
import com.ruoyi.yipaiju.service.IDiscountHouseSigningorderService;
import com.ruoyi.yipaiju.service.IForeclosedHouseSigningorderService;
import com.ruoyi.yipaiju.service.IHouseUploadingOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

@RestController
@RequestMapping("/market/manager")
public class MarketManagerController extends BaseController {

    @Autowired
    private IHouseUploadingOrderService houseUploadingOrderService;
    @Autowired
    private IDiscountHouseService discountHouseService;
    @Autowired
    private IForeclosedHouseSigningorderService foreclosedHouseSigningorderService;
    @Autowired
    private IDiscountHouseSigningorderService discountHouseSigningorderService;

    /**
     * 放盘签约审核
     * @param id
     * @return
     */
    @PostMapping("/houses/uploading/orders/{id}")
    public ApiResult houseuploadingOrdersInfo(@PathVariable("id") Long id, @RequestBody HouseUploadingOrder houseUploadingOrder) {
        HouseUploadingOrder houseUploadingOrder1 = houseUploadingOrderService.selectHouseUploadingOrderById(id);
        houseUploadingOrder1.setMarketManagerVertifyStatus(houseUploadingOrder.getMarketManagerVertifyStatus());
        houseUploadingOrder1.setRejectReason(houseUploadingOrder.getRejectReason());
        if (houseUploadingOrder1.getMarketManagerVertifyStatus().intValue() == 2){
            DiscountHouse discountHouse = discountHouseService.selectDiscountHouseById(houseUploadingOrder1.getDiscountHouseId());
            discountHouse.setPriceYuan(houseUploadingOrder.getPriceYuan().intValue());
            discountHouse.setStatus(1);
            // 计算单价和折扣
            discountHouse.setUnitPriceYuan(new BigDecimal((discountHouse.getPriceYuan().doubleValue() * 10000 / discountHouse.getBuiltUpAreaSquareCentimeter().doubleValue())).setScale(0, RoundingMode.HALF_DOWN));
            BigDecimal rate = new BigDecimal(((discountHouse.getPriceYuan().doubleValue() * 10000) / (discountHouse.getMarketPriceYuan().doubleValue() * 10000)) * 100).setScale(0, RoundingMode.HALF_DOWN);
            discountHouse.setDiscountRate(rate);
            discountHouseService.updateDiscountHouse(discountHouse);
            // TODO 拒绝和驳回应该也算完成 这个待确定  暂时先通过算 服务完成   1：待服务，2：服务中，3：待评论，4：已完成
            houseUploadingOrder1.setoStage(3);
            // 2：提交，3：审核，5：出售，7：完成  // TODO 好像不对。。
            houseUploadingOrder1.setStage(4);
            houseUploadingOrder1.setMarketManagerVerificationFinishTime(new Date());
        }
        int result = houseUploadingOrderService.updateHouseUploadingOrder(houseUploadingOrder1);
        if (result > 0) {
            return ApiResult.success(houseUploadingOrder1);
        }else {
            return ApiResult.error();
        }
    }

    /**
     * 优选签约审核  市场经理
     * @param id
     * @param foreclosedHouseSigningorder
     * @return
     */
    @PostMapping("/houses/foreclosed/signing/orders/{id}")
    public ApiResult housesForeclosedSigningOrdersInfo(@PathVariable("id") Long id, @RequestBody ForeclosedHouseSigningorder foreclosedHouseSigningorder) {
        ForeclosedHouseSigningorder foreclosedHouseSigningorder1 = foreclosedHouseSigningorderService.selectForeclosedHouseSigningorderById(id);
        // 驳回重新设置为未提交
        if (foreclosedHouseSigningorder.getMarketManagerVertifyStatus().intValue() == 3){
            foreclosedHouseSigningorder1.setSubmitStatus(0);
        }
        // 通过
        if (foreclosedHouseSigningorder.getMarketManagerVertifyStatus().intValue() == 2){
            // 审核通过时判断风险经理是否已经审核
            if (foreclosedHouseSigningorder1.getFinancialManagerVertifyStatus().intValue() != 2){
                return ApiResult.error("风险经理未审核");
            }
 
            if (foreclosedHouseSigningorder1.getStage().intValue() == 2){
                foreclosedHouseSigningorder1.setAuctionBeforeTime(new Date());
                foreclosedHouseSigningorder1.setStage(3);
            }else if (foreclosedHouseSigningorder1.getStage().intValue() == 3){
                foreclosedHouseSigningorder1.setAuctioningTime(new Date());
                foreclosedHouseSigningorder1.setStage(4);
            }else if (foreclosedHouseSigningorder1.getStage().intValue() == 5){
                foreclosedHouseSigningorder1.setAuctionAfterTime(new Date());
                foreclosedHouseSigningorder1.setStage(7);
            }

            foreclosedHouseSigningorder1.setSubmitStatus(0);
//            foreclosedHouseSigningorder1.setMarketManagerVertifyStatus(1);
//            foreclosedHouseSigningorder1.setFinancialManagerVertifyStatus(1);
        }

        foreclosedHouseSigningorder1.setMarketManagerVertifyStatus(foreclosedHouseSigningorder.getMarketManagerVertifyStatus());
        foreclosedHouseSigningorder1.setRejectReason(foreclosedHouseSigningorder.getRejectReason());

        int result = foreclosedHouseSigningorderService.updateForeclosedHouseSigningorder(foreclosedHouseSigningorder1);
        if (result > 0) {
            return ApiResult.success(foreclosedHouseSigningorder1);
        }else {
            return ApiResult.error();
        }
    }


    /**
     * 折扣签约审核  市场经理
     * @param id
     * @param discountHouseSigningorder
     * @return
     */
    @PostMapping("/houses/discount/signing/orders/{id}")
    public ApiResult housesDiscountSigningOrdersInfo(@PathVariable("id") Long id, @RequestBody DiscountHouseSigningorder discountHouseSigningorder) {
        DiscountHouseSigningorder discountHouseSigningorder1 = discountHouseSigningorderService.selectDiscountHouseSigningorderById(id);
        // 驳回重新设置为未提交
        if (discountHouseSigningorder.getMarketManagerVertifyStatus().intValue() == 3){
            discountHouseSigningorder1.setSubmitStatus(0);
        }
        // 通过
        if (discountHouseSigningorder.getMarketManagerVertifyStatus().intValue() == 2){
            // 审核通过时判断风险经理是否已经审核
            if (discountHouseSigningorder1.getFinancialManagerVertifyStatus().intValue() != 2){
                return ApiResult.error("风险经理未审核");
            }

            if (discountHouseSigningorder1.getStage().intValue() == 2){
                discountHouseSigningorder1.setAuctionBeforeTime(new Date());
                discountHouseSigningorder1.setStage(3);
            }else if (discountHouseSigningorder1.getStage().intValue() == 3){
                discountHouseSigningorder1.setAuctioningTime(new Date());
                discountHouseSigningorder1.setStage(4);
            }else if (discountHouseSigningorder1.getStage().intValue() == 4){
                discountHouseSigningorder1.setAuctionDealTime(new Date());
                discountHouseSigningorder1.setStage(5);
            }else if (discountHouseSigningorder1.getStage().intValue() == 5){
                discountHouseSigningorder1.setAuctionAfterTime(new Date());
                discountHouseSigningorder1.setStage(7);
            }

            discountHouseSigningorder1.setSubmitStatus(0);
//            foreclosedHouseSigningorder1.setMarketManagerVertifyStatus(1);
//            foreclosedHouseSigningorder1.setFinancialManagerVertifyStatus(1);
        }

        discountHouseSigningorder1.setMarketManagerVertifyStatus(discountHouseSigningorder.getMarketManagerVertifyStatus());
        discountHouseSigningorder1.setRejectReason(discountHouseSigningorder.getRejectReason());

        int result = discountHouseSigningorderService.updateDiscountHouseSigningorder(discountHouseSigningorder1);
        if (result > 0) {
            return ApiResult.success(discountHouseSigningorder1);
        }else {
            return ApiResult.error();
        }
    }

}
